//set main namespace
goog.provide('WeaponFactory');

//get requirements
goog.require('lime.animation.FadeTo');
goog.require('lime.animation.ScaleTo');
goog.require('lime.animation.MoveTo');
goog.require('lime.RoundedRect');
goog.require('lime.Node');

goog.require('goog.math.Box');
goog.require('goog.net.Jsonp');
goog.require('goog.events.EventType');

goog.require('GLL');
goog.require('GLL.Game');
goog.require('GLL.events.Rotate');
goog.require('GLL.Rect');


// entrypoint
WeaponFactory.start = function()
{
	GLL.init();
	//var director = new lime.Director(document.body,GLL.Rsltn.w, GLL.Rsltn.h);
	//director.makeMobileWebAppCapable();
	GLL.newScene('mod_std');
	GLL.useScene('mod_std');
	
	//
	var grid_step = 20;
	//
	var bkgrnd_size = (GLL.Rsltn.w > GLL.Rsltn.h)? GLL.Rsltn.h : GLL.Rsltn.w;
	bkgrnd_size -= grid_step * 2;
	var grid_size = bkgrnd_size / grid_step;
	var grid_hsize = grid_size / 2;	
	var bkgrnd_x = (GLL.Rsltn.w - bkgrnd_size)/2;
	var bkgrnd_y = (GLL.Rsltn.h - bkgrnd_size)/2;
	
	var bkgrnd = new lime.Sprite().setFill('#99FF99').setAnchorPoint(0,0).setPosition(bkgrnd_x, bkgrnd_y).setSize(bkgrnd_size, bkgrnd_size);
	GLL.addToScene(bkgrnd);
	
	var grids = [];

	for(var x = 0; x < bkgrnd_size; x+=grid_size)
	{
		grids[x] = [];
		for(var y = 0; y < bkgrnd_size; y+=grid_size)
		{
			var rect = new GLL.Rect(bkgrnd).setPosition(x + grid_hsize, y + grid_hsize)
			.setSize(grid_size, grid_size).setStroke(1,'#990000');
		}
	}
	
	//GLL.addToScene(rect);
	
	return;
	var anc_x = 61, anc_y = 18;
	//-------------------------------------------------------------------------------------
	var machine_x = 100;
	var machine_base = GLL.newImage('machine_base').setPosition(GLL.Rsltn.hw, GLL.Rsltn.hh);
	//machine_base.setAnchorPoint(0, 0);
	machine_base.setScale(5);

	var indicator_rotate = GLL.newImage('rotate').setOpacity(0);
	
	var machine_gun = GLL.newImage('gun1', machine_base).setAnchorPoint(0, 0);
	machine_gun.setPosition(0, 0).setRotation(45);
	machine_gun._captured = false;
	machine_gun.setAnchorPoint(.5,.5);
	
	var fade_show = new lime.animation.FadeTo(0.5).setDuration(.2);
	var fade_hide = new lime.animation.FadeTo(0);
	//var anim_show = new lime.animation.Spawn(fade_show);
	//var anim_hide = new lime.animation.Spawn(fade_hide);
	//goog.events.listen(machine_base, )
	var time = 0;

	function TEST(e){
		console.log(time++);
	}
	
	goog.events.listen(machine_gun, GLL.EVENT_MD, function(e)
	{
		var btn = e.event.button;
		var mod = e.target;
		
		var rotating = false;
		var moving = false;
		if(btn == goog.events.BrowserEvent.MouseButton.RIGHT) rotating = true;
		else moving = true;
		
		if(rotating)
		{
			mod.appendChild(indicator_rotate);
			indicator_rotate.runAction(fade_show);
			
			var rot_event = new GLL.events.Rotate(e);
			goog.events.listen(rot_event, GLL.events.Rotate.Event.ROTATE, function(e){
				console.log('rot:' + e.rotation);
			})
			goog.events.listen(rot_event, GLL.events.Rotate.Event.END, function(e){
				indicator_rotate.runAction(fade_hide);
			})
		}
		else if(moving)
		{
			e.startDrag();
			//var dragevent = new lime.events.Drag(e);
			//goog.events.listen(dragevent, lime.events.Drag.Event.MOVE, function(e)
			//goog.events.listen(mod, EVENT_MMOVE, function(e){
				var pos = mod.getPosition();
				//var pos_x = event.screenPosition.x;
        		//var pos_y = event.screenPosition.y;
        		//mod.setPosition(pos_x, pos_y);
 				//console.log(pos);
			//});
		}
		
	
	});
	
	

    

}


//this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode
goog.exportSymbol('WeaponFactory.start', WeaponFactory.start);
